package com.xingdata.jjxc.activity;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.xingdata.jjxc.App;
import com.xingdata.jjxc.R;
import com.xingdata.jjxc.adapter.CarTypeListAdapter;
import com.xingdata.jjxc.base.Base1Activity;
import com.xingdata.jjxc.enty.AddCarEntity;
import com.xingdata.jjxc.enty.CarBrandEntity;
import com.xingdata.jjxc.enty.RespEntity;
import com.xingdata.jjxc.http.HttpResponseListener;

public class CarTypeActivity extends Base1Activity implements OnClickListener {
	private TextView title;
	private ImageView titleImageView;
	private ListView addcarlist;
	private CarTypeListAdapter adapter;
	private List<CarBrandEntity> brandEntities = new ArrayList<CarBrandEntity>();
	private List<AddCarEntity> addCarEntities = new ArrayList<AddCarEntity>();
	private String brandId;
	private String cartype_tv;
	//车型号id
	private String car_model_id;
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		initEntity();
	}

	@Override
	protected int getContentViewId() {

		return R.layout.activity_car_type;
	}

	@Override
	protected String getTitleText() {
		return "选择车型";
	}

	@Override
	protected void initViews() {
		((ImageView) findViewById(R.id.title_back)).setOnClickListener(this);
		titleImageView = (ImageView) findViewById(R.id.title_delet);
		titleImageView.setVisibility(View.VISIBLE);
		titleImageView.setOnClickListener(this);
		addcarlist = (ListView) findViewById(R.id.cartypelist);
		// Bundle bundle=getIntent().getExtras();
		// String carbranch_id=bundle.getString("carbranch_id");
		Bundle bundle=getIntent().getExtras();
		brandId= bundle.getString("brand_id");
		
		
		doPost_getcarmodellist();
		adapter = new CarTypeListAdapter(this, addCarEntities);
		addcarlist.setAdapter(adapter);
		addcarlist.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
//				cartype_tv=(String)((TextView)arg1.findViewById(R.id.cartype_tv)).getText();
//				Toast.makeText(getApplicationContext(), cartype_tv,
//					     Toast.LENGTH_SHORT).show();
//				car_model_id=addcarlist.getItemAtPosition(arg2).toString();
				Intent intent=new Intent();
				Bundle bundle=new Bundle();
				AddCarEntity carEntity=new AddCarEntity();
				carEntity.setCar_model_id(addCarEntities.get(arg2).getCar_model_id());
				carEntity.setCar_model_name(addCarEntities.get(arg2).getCar_model_name());
				bundle.putSerializable("carEntity", carEntity);
				intent.putExtras(bundle);
//				intent.putExtra("okresoult", cartype_tv);
//				intent.putExtra("car_model_id", car_model_id);
				setResult(RESULT_OK, intent);
				finish();
			}
		});
		
	}

	// @SuppressWarnings({ "unchecked", "unused" })
	private void initEntity() {
//		addCarEntities.addAll((Collection<? extends AddCarEntity>) getIntent()
//				.getExtras().getParcelableArrayList("brand1"));
		adapter.notifyDataSetChanged();
	}

	/**
	 * 获取车辆型号列表
	 */
	private void doPost_getcarmodellist() {
		paramsMap = new HashMap<String, String>();
			paramsMap.put("car_branch_id", brandId);

		mHttpUtil.Post(App.ZZD_REQUEST_GETCARMODELLIST, paramsMap,
				new HttpResponseListener() {

					@Override
					public void onSuccess(RespEntity respEntity) {
					
						addCarEntities.addAll(JSON.parseArray(
								respEntity.getResult(), AddCarEntity.class));
//						Intent intent=new Intent();
						
//						setResult(RESULT_OK,intent);
						adapter.notifyDataSetChanged();
//						System.out.print(addCarEntities);
//						finish();
					}

					@Override
					public void onLoading(int vurrent) {

					}

					@Override
					public void onFailure(String result) {
						
					}
				});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title:
		case R.id.title_back:
			finish();
			break;

		default:
			break;
		}
	}

}
